package com.customization.unionpay.market.util;

import com.alibaba.fastjson.JSONObject;
import com.unionpay.magpie.bootstrap.MagpieBootStrap;
import com.unionpay.magpie.client.ServiceController;
import com.unionpay.magpie.client.ServiceRegistry;
import com.unionpay.magpie.common.Constants;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import java.nio.charset.StandardCharsets;

/**
 * @author sjm
 * @description 调用内网magpie平台相关
 */
public class MagpieUtils {

    private static final Logger log = LoggerFactory.getLogger(MagpieUtils.class);

    /**
     * 调用云闪付相关
     * @return
     */
    public static String getCloudFlashPayment(JSONObject params,String serviceId) throws Exception {
        log.info("params: "+params);
        System.setProperty(Constants.MAGPIE_CONFIG_FILE_KEY,"magpie.xml");
        log.info("loading...");
        MagpieBootStrap.getBootStrap().start();

        if(serviceId == null){
            serviceId = "m2vop_callOpenApi";
        }
        log.info("change Data ...");
        // 转换
        byte[] bytes = params.toJSONString().getBytes(StandardCharsets.UTF_8);
        log.info("all params yun: " + new String(bytes));
        // 获取magpie的服务
        log.info("start magpie yun");
        byte[] result;
        ServiceController outputapi= ServiceRegistry.getService(serviceId);
        result = outputapi.call(bytes);
        log.info("result: " + new String(result));
        // 云闪付字符编码
        String valueCodeYun = com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "valueCodeYun");
        return  new String(result,valueCodeYun);
    }

    /**
     * 调用清算业务核算系统相关
     * @return
     */
    public static String getClearBusAccSystem(JSONObject params,String serviceId) throws Exception {
        log.info("params: "+params);
        System.setProperty(Constants.MAGPIE_CONFIG_FILE_KEY,"magpie.xml");
        log.info("loading...");
        MagpieBootStrap.getBootStrap().start();

        if(serviceId == null){
            serviceId = "fcl_chargeUpByAcctNo";
        }
        log.info("change Data ...");
        // 转换
        byte[] bytes = params.toJSONString().getBytes(StandardCharsets.UTF_8);
        log.info("all params clear: " + new String(bytes));
        // 获取magpie的服务
        log.info("start magpie clear");
        byte[] result;
        ServiceController outputapi= ServiceRegistry.getService(serviceId);
        result = outputapi.call(bytes);
        log.info("result: " + new String(result));
        // 清算业务核算系统编码
        String valueCodeClear = com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "valueCodeClear");
        return  new String(result,valueCodeClear);

    }

    /**
     * 调用创新结算系统相关
     * @return
     */
    public static String getCreateSystemSettlement(JSONObject params,String serviceId) throws Exception {
        log.info("params: "+params);
        System.setProperty(Constants.MAGPIE_CONFIG_FILE_KEY,"magpie.xml");
        log.info("loading...");
        MagpieBootStrap.getBootStrap().start();

        if(serviceId == null){
            serviceId = "up_fmoswbx_transpayservice";
        }
        log.info("change Data ...");
        // 转换
        byte[] bytes = params.toJSONString().getBytes(StandardCharsets.UTF_8);
        log.info("all params Inn: " + new String(bytes));
        // 获取magpie的服务
        log.info("start magpie Inn");
        byte[] result;
        ServiceController outputapi= ServiceRegistry.getService(serviceId);
        result = outputapi.call(bytes);
        log.info("result: " + new String(result));
        // 清算业务核算系统编码
        String valueCodeClear = PropUtils.BB_LOG.getPropValue("marketSubject", "valueCodeInn");
        return  new String(result,valueCodeClear);
    }

}
